--    Program: 		CAPPOIMP_DOCUMENT_TYPE_TL.sql
--    Description:  Import PO Document Type information 
--    Revision:     1st     
--    Parameter: 	
--    Created By:   Kenny Leung
--    Date:         23-Jan-2006

set doc off;      
set verify off;
set serveroutput on size 1000000;
whenever sqlerror exit failure rollback;

declare   
   l_processed_count		number;
   l_rejected_count 		number;
   l_err_msg				varchar2(2000);
   l_err_msg_tmp			varchar2(2000);
                    		                 		
   cursor cur_po_document_type is
      select * from CRST_PO_DOCUMENT_TYPES_ALL_TL;

begin
   l_processed_count := 0;
   l_rejected_count  := 0;
   l_err_msg := '';
   
   for cur in cur_po_document_type loop   	
     l_err_msg_tmp := '';
     
     begin
       -- import data
       insert into PO_DOCUMENT_TYPES_ALL_TL
       (
              DOCUMENT_TYPE_CODE  ,
              DOCUMENT_SUBTYPE    ,
              ORG_ID              ,
              LANGUAGE            ,
              SOURCE_LANG         ,
              TYPE_NAME           ,
              LAST_UPDATE_DATE    ,
              LAST_UPDATED_BY     ,
              CREATED_BY          ,
              CREATION_DATE       ,
              LAST_UPDATE_LOGIN    
       )
       values
       (
                cur.DOCUMENT_TYPE_CODE  ,
                cur.DOCUMENT_SUBTYPE    ,
                CAPBRIMP_MAPPING.GetOrgMapping(cur.ORG_ID),
                cur.LANGUAGE            ,
                cur.SOURCE_LANG         ,
                cur.TYPE_NAME           ,
                cur.LAST_UPDATE_DATE    ,
                CAPBRIMP_MAPPING.GetUserMapping(cur.LAST_UPDATED_BY),
                CAPBRIMP_MAPPING.GetUserMapping(cur.CREATED_BY),
                cur.CREATION_DATE       ,
                cur.LAST_UPDATE_LOGIN   
       );
     
	 exception
       when others then
     	 l_err_msg_tmp := sqlerrm;
     end;	  	    
     
     if l_err_msg_tmp IS NULL then
       l_processed_count := l_processed_count + 1;  
     else
       l_rejected_count := l_rejected_count + 1;
       l_err_msg := l_err_msg_tmp; 
     end if;
   end loop;

   if l_rejected_count = 0 then
   	 commit;
   else
     rollback;
   end if;
   
   -- insert into log table
   insert into capcnv_log ( prog_id,process_date, record_processed,record_rejected,record_extracted,err_msg) 
    values ('CAPPOIMP_DOCUMENT_TYPE_TL', sysdate, l_processed_count, l_rejected_count, l_processed_count-l_rejected_count, l_err_msg );
   
   commit;
exception 
  when others then
    dbms_output.put_line(sqlerrm);
    rollback;  
end;
/


--exit
